namespace KinonekoSoftware.CodariaDB.Documents.DataParts.Basics
{
    [DebuggerDisplay("{Name}({Metadata}) -> {Value}")]
    public sealed class CompositeProperty : CompositeItem
    {
        private string _value;

        public override Variant GetVariant(string documentID)
        {
            var id = Id;
            
            if (string.IsNullOrEmpty(id))
            {
                id = ID.Get();
            }
            return new StringVariant
            {
                Id       = id,
                DocumentID = documentID,
                Metadata = Metadata,
                Value    = Value,
                Type     = VariantType.String,
            };
        }

        public override CompositeItem Clone()
        {
            var id = Id;
            
            if (string.IsNullOrEmpty(id))
            {
                id = ID.Get();
            }

            return new CompositeProperty
            {
                Id          = id,
                Metadata    = Metadata,
                IsRemovable = IsRemovable,
                IsEditable  = IsEditable,
                Value       = Value,
            };
        }

        public string Value
        {
            get => _value;
            set
            {
                SetValue(ref _value, value);
                OnValueChanged?.Invoke(this);
            }
        }
    }
}